package com.example.demo.web;

import com.example.demo.module.log.LogBean;
import com.example.demo.module.vo.PhoneBeanVo;
import com.example.demo.service.LogService;
import com.example.demo.web.util.HttpUtils;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:lgl
 * @Description:
 * @Date:Create in 2017/11/17 .
 */
@Controller
public class PhoneController {

    @GetMapping("phone")
    @ResponseBody
    public String phone(String phone) {
        String host = "http://jshmgsdmfb.market.alicloudapi.com";
        String path = "/shouji/query";
        String method = "GET";
        String appcode = "bb2dd76f276e46c5bff366696ca66316";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("shouji", phone);
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String str = EntityUtils.toString(response.getEntity());
            Gson gson = new Gson();
            PhoneBeanVo phoneBeanVo = gson.fromJson(str,PhoneBeanVo.class);
            System.out.println(phoneBeanVo+"**********");

            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }

    @Autowired private LogService logService;

    @GetMapping("test")
    @ResponseBody
    public List<LogBean> a(String b){
        List<LogBean> list = logService.selectAll();
        return  list;
    }

}
